<template>
  <div class="block-box">
    <div class="taskdetail">
      <h3 class="task-title">
        任务进度 <img class="icon" :src="getImageUrl('leftbg10.png')" alt="" />
      </h3>
      <!-- <ElRow class="task-message" :gutter="15">
        <ElCol class="cabin-img" :span="8">
          <section style="
              background-image: url('../../../../../../assets/flaybg1.png');
            ">
            <p class="task-bold">4</p>
            <p class="">航点数</p>
          </section>
        </ElCol>
        <ElCol class="cabin-img" :span="8">
          <section style="
              background-image: url('../../../../../../assets/flaybg2.png');
            ">
            <p class="task-bold">6.21km</p>
            <p class="">预计飞行里程</p>
          </section>
        </ElCol>
        <ElCol class="cabin-compass" :span="8">
          <section style="
              background-image: url('../../../../../../assets/flaybg3.png');
            ">
            <p class="task-bold">15min12s</p>
            <p class="">预计飞行时长</p>
          </section>
        </ElCol>
      </ElRow> -->
      <div class="task-message">
        <div class="cabin-img" :style="'background-image: url(' + getImageUrl('flaybg1.png') + ');'">
          <p class="task-bold">4</p>
          <p class="">航点数</p>
        </div>
        <div class="cabin-img" :style="'background-image: url(' + getImageUrl('flaybg2.png') + ');'">
          <p class="task-bold">6.21km</p>
          <p class="">预计飞行里程</p>
        </div>
        <div class="cabin-img" :style="'background-image: url(' + getImageUrl('flaybg3.png') + ');'">
          <p class="task-bold">15min12s</p>
          <p class="">预计飞行时长</p>
        </div>
      </div>
      <!-- <el-scrollbar wrapClass="scrollbar-wrap" style="height: 48vh; margin-top: 1vh" ref="scrollbarContainer">
        <el-timeline>
          <el-timeline-item v-for="(activity, index) in activities" :key="index" :icon="activity.icon"
            :type="activity.type" :color="activity.color" :size="activity.size" :timestamp="activity.timestamp">
              <div class="timeline-content">
                <p>{{ activity.content }}</p>
                <img class="task-img-icon" src="../../../../../assets/flaybg11.png" alt="" />
              </div>
              <div class="timeline-img" v-if="activity.title">
                <div>
                  <img class="task-img-icon" src="../../../../../assets/home.png" alt="" />
                </div>
                <div>
                  <img class="task-img-icon" src="../../../../../assets/home.png" alt="" />
                </div>
                <div class="title">{{ activity.title }}</div>
              </div>
          </el-timeline-item>
        </el-timeline>
      </el-scrollbar> -->
      <div class="scrollbar-wrap">
        <div class="timeline-box">
          <div class="timeline-item" v-for="(item, index) in props.taskList" :key="index">
            <div class="time-box">
              {{ item.createTime?item.createTime.split(' ')[1]:'' }}
            </div>
            <div class="circle-box">
              <div class="circle">
                <div class="line" :class="activities_computed(index)"></div>
              </div>
            </div>
            <div class="timeline-content">
              <div class="up-box">
                <p>{{ item.processName }}</p>
                <img class="task-img-icon" src="../../../../../assets/flaybg11.png" alt="" />
              </div>
              <div class="down-box">
                <img v-show="item.icon1" class="task-img-icon" src="../../../../../assets/dp_rw_icon.png" alt="" />
                <img v-show="item.icon2" class="task-img-icon" src="../../../../../assets/dp_rw_icon1.png" alt="" />
                <img v-show="item.icon3" class="task-img-icon" src="../../../../../assets/dp_rw_icon2.png" alt="" />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="detail-map">
      <h3 class="task-title">
        航线地图 <img class="icon" :src="getImageUrl('leftbg10.png')" alt="" />
      </h3>
      <div class="map_box">
        <div class="map" id="map-container"></div>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import {
  ElRow,
  ElCol,
  ElButton,
  ElTimelineItem,
  ElTimeline,
  ElScrollbar,
} from 'element-plus';
import { ref, onMounted, onBeforeUnmount, defineProps, computed } from 'vue';
// import Hls from 'hls.js';
import { useRouter } from 'vue-router';
import { openWindow } from '@vben/utils';
const getImageUrl = (name: string) => {
  return new URL(`../../../../../assets/${name}`, import.meta.url).href
}
const props = defineProps({ airlineData: Object, taskList: Array });
const router = useRouter();
const getSpan = (index) => {
  switch (props.changeViewId) {
    case 1:
      return 24;
    case 2:
      return 12;
    case 3:
      return index === 0 ? 16 : 8;
    default:
      return 8;
  }
};
const activities_computed = (currentIndex) => {
  const nextActivity = props.taskList[currentIndex + 1]
  const hasIcon = nextActivity?.icon1 || nextActivity?.icon2 || nextActivity?.icon3
  return hasIcon ? 'line1' : 'line2'
}
const activities = ref([
  {
    content: '任务下发',
    timestamp: '20:46',
    icon1: '',
    icon2: '',
    icon3: '',
  },
  {
    content: '系统监测',
    timestamp: '20:46',
    icon1: '',
    icon2: '',
    icon3: '',
  },
  {
    content: '无人机起飞',
    timestamp: '20:46',
    icon1: '',
    icon2: '',
    icon3: '',
  },
  {
    content: '无人机到达航点1',
    timestamp: '20:46',
    icon1: '1',
    icon2: '1',
    icon3: '',
  },
  {
    content: '无人机到达航点2',
    timestamp: '20:46',
    size: 'normal',
    title: '人流检测',
    icon1: '1',
    icon2: '1',
    icon3: '1',
  },
  {
    content: '无人机到达航点3',
    timestamp: '20:46',
    size: 'normal',
    title: '人流检测',
    icon1: '1',
    icon2: '1',
    icon3: '1',
  },
  {
    content: '无人机前往航点4',
    timestamp: '20:46',
    size: 'normal',
    title: '人流检测',
    icon1: '1',
    icon2: '',
    icon3: '',
  },
]);

// let hlsInstances: Record<number, Hls> = {};

const navToClick = () => {
  console.log('bbbbb');
  return;
  let nav = '/dashboard/dronemonitoring/detail/module/index.vue';
  if (nav?.startsWith('http')) {
    openWindow(nav);
    return;
  }
  if (nav?.startsWith('/')) {
    router.push(nav).catch((error) => {
      console.error('Navigation failed:', error);
    });
  } else {
    console.warn(`Unknown URL for navigation item: ${nav.title} -> ${nav.url}`);
  }
};
const loadAMap = () => {
  return new Promise((resolve, reject) => {
    if (window.AMap) {
      resolve(window.AMap);
      return;
    }
    const script = document.createElement('script');
    script.src = `https://webapi.amap.com/maps?v=2.0&key=ce77626588b7a2ee836159f48dd4a1a6`;
    script.addEventListener('load', () => resolve(window.AMap));
    script.onerror = reject;
    document.head.append(script);
  });
};
let map;
let centerlnglat;
onMounted(async () => {
  // props.droneList.forEach((item, index) => {
  //   const videoElement = document.getElementById(`video-${index}`);
  //   if (videoElement && Hls.isSupported()) {
  //     const hls = new Hls();
  //     hls.loadSource(item.streamUrl); // 假设每个无人机项都有一个 streamUrl 属性
  //     hls.attachMedia(videoElement);
  //     hls.on(Hls.Events.MANIFEST_PARSED, function () {
  //       videoElement.play();
  //     });
  //     hlsInstances[index] = hls;
  //   } else if (
  //     videoElement &&
  //     videoElement.canPlayType('application/vnd.apple.mpegurl')
  //   ) {
  //     videoElement.src = item.streamUrl; // 假设每个无人机项都有一个 streamUrl 属性
  //     videoElement.addEventListener('canplay', function () {
  //       videoElement.play();
  //     });
  //   }
  // });
  try {
    await loadAMap();
    centerlnglat = [119.904792, 30.247956]
    if (!map) {
      map = new AMap.Map('map-container', {
        zoom: 15,
        center: centerlnglat,
        pitch: 60, // 俯仰角度
        viewMode: '3D', // 启用 3D 模式
        layers: [
          // 卫星
          new AMap.TileLayer.Satellite(),
          // 路网
          new AMap.TileLayer.RoadNet(),
        ],
      });
    }
  } catch (error) {
    console.error('地图加载失败:', error);
  }
});
onBeforeUnmount(() => {
  // Object.values(hlsInstances).forEach((hls) => {
  //   hls.destroy();
  // });
});
</script>

<style lang="scss" scoped>
.block-box {
  width: 100%;
  height: 100%;

  .task-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 4vh;
    padding-left: 1.5vh;
    margin: 0;
    font-family: PingFangSC, 'PingFang SC';
    font-size: 2vh;
    font-weight: 500;
    color: #fffefe;
    user-select: none;
    background: radial-gradient(ellipse 140% 300% at 100% 50%,
        rgba(1, 34, 35, 0.2) 28%,
        rgba(7, 217, 221, 0.7) 100%);

    .icon {
      width: 3vh;
      margin-right: 1vw;
    }
  }
}

.taskdetail {
  background: rgb(2 19 20 / 52%);
  border: 0.01vh solid;
  border-image: radial-gradient(circle,
      rgb(36 229 241 / 17%),
      rgb(255 255 255 / 17%)) 1 1;
  height: 59vh;

  .task-line {
    width: 100%;
    // height: 1px;
    // margin-top: 20px;
    background: #fff;
  }

  .task-message {
    // padding: 0 1rem;
    // text-align: center;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .cabin-img {
      background-size: 100% 100%;
      background-repeat: no-repeat;
      width: calc(33.333333% - 1vh);
      margin: 0 0.5vh;
      margin-top: 1vh;
      height: 3.2vw;
      padding-left: 3.5vw;
      display: flex;
      flex-direction: column;
      justify-content: center;

      p {
        // padding: 0.3rem 0;
        // margin-left: 3.3rem;
        // font-size: 14px;
        font-size: 0.7vw;
      }

      .task-bold {
        // margin-top: 1rem;
        // font-size: 16px;
        font-weight: bold;
      }
    }
  }

  .scrollbar-wrap {
    height: 48vh;
    margin-top: 1vh;

    .timeline-box {
      width: 100%;
      height: calc(100% - 2vh);
      overflow-y: auto;

      .timeline-item {
        display: flex;
        align-items: center;
        margin-top: 2vh;

        .time-box {
          background-image: url('../../../../../assets/flaybg4.png');
          background-size: 100% 100%;
          background-repeat: no-repeat;
          padding: 0.5vh 2.5vh 0.5vh 0.7vh;
          font-size: 1.6vh;
          margin-left: 5vh;
        }

        .circle-box {
          border-radius: 2vh;
          border: 0.2vh solid #00BEC2;
          margin: 0 1.5vh;

          .circle {
            background-color: #00BEC2;
            width: 1.2vh;
            height: 1.2vh;
            border-radius: 1.2vh;
            margin: 0.2vh;
            position: relative;
            display: flex;
            align-items: center;
            justify-content: center;

            .line {
              width: 0.2vh;
              position: absolute;
              top: 1.2vh;
              left: 0.5vh;
            }

            .line1 {
              height: 8vh;
              background: radial-gradient(ellipse 100% 100% at 50% 0%, #00BEC2 28%, rgb(2, 43, 47) 100%);
            }

            .line2 {
              height: 4.7vh;
              background: radial-gradient(ellipse 100% 100% at 50% 0%, #00BEC2 28%, rgb(2, 43, 47) 100%);
            }
          }
        }

        .timeline-content {
          background: radial-gradient(ellipse 140% 300% at 100% 50%,
              rgba(9, 63, 68, 1) 28%,
              rgba(2, 43, 47, 1) 100%);
          padding: 1vh;
          padding-left: 2vh;
          width: calc(100% - 20vh);

          .up-box {
            display: flex;
            align-items: center;
            justify-content: space-between;

            p {
              font-size: 1.8vh;
            }

            .task-img-icon {
              width: 2vh;
              height: 2vh;
            }
          }

          .down-box {
            display: flex;
            align-items: center;
            padding-top: 0.5vh;

            .task-img-icon {
              width: 2.5vh;
              height: 2.5vh;
              margin-right: 0.5vh;
            }
          }
        }
      }
    }
  }

  // .timeline-content {
  //   display: flex;
  //   justify-content: space-between;

  //   .task-img-icon {
  //     width: 2vh;
  //     height: 2vh;
  //   }

  //   p {
  //     font-size: 1.8vh;
  //   }
  // }

  // .timeline-img {
  //   display: flex;
  //   align-items: center;
  //   margin-top: 0.5vh;

  //   div {
  //     padding: 0.5vh;
  //     margin-right: 0.5vh;
  //     border-radius: 0.01vh;
  //     border-image: radial-gradient(circle,
  //         rgb(36 229 241 / 52%),
  //         rgb(255 255 255 / 52%)) 1 1;
  //     // opacity: 0.52;
  //   }

  //   .title {
  //     padding: 0.5vh;
  //     font-size: 1.5vh;
  //   }

  //   .task-img-icon {
  //     width: 2.5vh;
  //     height: 2.5vh;
  //   }
  // }

  // :deep(.el-scrollbar) {
  //   height: 90%;

  //   .scrollbar-wrap {
  //     overflow-x: hidden;
  //   }

  //   .el-scrollbar__view {
  //     padding: 0 1vh;
  //   }
  // }

  // .el-timeline {
  //   margin-top: 1vh;
  //   margin-left: 13vh;

  //   :deep(.el-timeline-item) {
  //     padding-bottom: 1vh;
  //   }

  //   :deep(.el-timeline-item__timestamp.is-bottom) {
  //     position: absolute;
  //     top: 0.6vh;
  //     left: -9vh;
  //     // padding: 0.3rem 1.1rem 0.3rem 0.3rem;
  //     padding: 0.5vh 2vh 0.5vh 1vh;
  //     margin-top: 0;
  //     font-size: 2vh;
  //     font-weight: bold;
  //     color: #fffefe;
  //     background-image: url('../../../../../../assets/flaybg4.png');
  //     background-repeat: no-repeat;
  //     background-size: 100% 100%;
  //   }

  //   :deep(.el-timeline) {
  //     padding-left: 150px;
  //   }

  //   :deep(.el-timeline-item__tail) {
  //     border: 1px solid;
  //     border-image: linear-gradient(135deg,
  //         rgb(0 190 194 / 100%) 0%,
  //         rgb(2 39 42 / 0%) 100%) 1 1;
  //   }

  //   :deep(.el-timeline-item__node) {
  //     position: relative;
  //     left: -2px;
  //     padding: 6px;
  //     background: transparent;
  //     /* 外层透明背景 */
  //     border: 1px solid #00bec2;
  //     /* 外层边框 */
  //   }

  //   :deep(.el-timeline-item__node)::after {
  //     position: absolute;
  //     top: 50%;
  //     left: 50%;
  //     width: 8px;
  //     height: 8px;
  //     content: '';
  //     background: #00bec2;
  //     /* 内层实心圆 */
  //     border-radius: 50%;
  //     transform: translate(-50%, -50%);
  //   }

  //   :deep(.el-timeline-item__content) {
  //     padding: 0.5rem 0.5rem 0.5rem 1rem;
  //     margin-right: 0.8rem;
  //     // margin-bottom: 1rem;
  //     font-size: 16px;
  //     color: #fff;
  //     background: radial-gradient(20% 0% at 0% 0%, #093f44 0%, #022b2f 100%);
  //   }

  //   :deep(.el-timeline-item__wrapper) {
  //     top: -1.5rem;
  //   }
  // }
}

.detail-map {
  width: 100%;
  height: 29.5vh;
  margin-top: 0.5vh;
  background: rgb(2 19 20 / 52%);
  border: 0.01vh solid;
  border-image: radial-gradient(circle,
      rgb(36 229 241 / 17%),
      rgb(255 255 255 / 17%)) 1 1;

  .map_box {
    width: 100%;
    height: calc(25.5vh);
    padding: 0.5vh;

    :deep(.amap-logo) {
      display: none !important;
    }

    :deep(.amap-copyright) {
      display: none !important;
    }


    .map {
      width: 100%;
      height: 100%;
    }
  }
}

.cabin-img section {
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

.cabin-compass section {
  background-repeat: no-repeat;
  background-size: 100% 100%;
}
</style>
